<?php
//显示方法
function display($tpl_path,$data)
{
	$TemplateDir = SERVER_ROOT.'view/';
	$CompileDir = SERVER_ROOT.'compile/';

	clear_comp($CompileDir,86400);	//清理过期的编译文件

	$temp_path = $TemplateDir.$tpl_path;

	if(is_file($temp_path))
	{
		$comp_file_name = md5($temp_path.'&'.filesize($temp_path)).'.php';
		$comp_file_path = $CompileDir.$comp_file_name;
		
		if(is_file($comp_file_path))
		{
			require_once($comp_file_path);
		}else{
			html_action($temp_path,$comp_file_path,$data);	//处理代码生成编译模板文件
			require_once($comp_file_path);
		}
	}else{
		echo $tpl_path.'模板文件错误';
		exit();
	}
}

//清理过期的编译文件
function clear_comp($CompileDir,$failure_time)
{
	$file_list = scandir($CompileDir);

	foreach($file_list as $file_name)
	{
		if(strlen($file_name)>3)
		{
			$make_time = filemtime($CompileDir.$file_name);

			if($make_time < (time()-$failure_time))
			{
				@unlink($CompileDir.$file_name);
			}
		}		
	}
}

//处理代码生成编译模板文件
function html_action($template_path,$compile_path,$data)
{
	$fileinfo = strip_whitespace(file_get_contents($template_path));
	$fileinfo = str_replace("<?=","<?php echo ",$fileinfo);

	foreach($data as $val=>$key) {
		$fileinfo = str_replace("$".$val,"\$data['".$val."']",$fileinfo);
	}

	file_put_contents($compile_path,$fileinfo);
}
?>